//
//  LYCoinsRechargeCell.m
//  Btpin
//
//  Created by liuyan on 2019/4/22.
//  Copyright © 2019年 Vic. All rights reserved.
//

#import "LYCoinsRechargeCell.h"
#import "LYCoinsRechargeItemCell.h"

@interface LYCoinsRechargeCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger selectIndex;

@end


@implementation LYCoinsRechargeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

#pragma mark - InitSubViews
- (void)initSubView{
    self.selectIndex = 0;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, WIDTH - 30, 20)];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textColor = RGB(34, 34, 34);
    titleLabel.text = @"充值聘豆";
    [self.contentView addSubview:titleLabel];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGRect frame = CGRectMake(0,titleLabel.bottom+10, WIDTH, 0);
    layout.itemSize = CGSizeMake((WIDTH - 45)/2, 70);
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 15;
    _collectview = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
    _collectview.backgroundColor = [UIColor whiteColor];
    _collectview.showsHorizontalScrollIndicator = NO;
    _collectview.showsVerticalScrollIndicator = NO;
    _collectview.delegate=self;
    _collectview.dataSource=self;
    [_collectview registerClass:[LYCoinsRechargeItemCell class] forCellWithReuseIdentifier:@"LYCoinsRechargeItemCell"];
    [self.contentView addSubview:_collectview];
}

#pragma mark -- UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LYCoinsRechargeItemCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"LYCoinsRechargeItemCell" forIndexPath:indexPath];
    [cell setData:self.dataArray[indexPath.row] isSelect:(indexPath.row == self.selectIndex)?YES:NO];
    return cell;
}

#pragma mark -- UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    _block(indexPath.row);
}

#pragma mark -- UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 15, 10,15);
}

#pragma mark -- Data
-(void)setData:(NSArray *)array selectIndex:(NSInteger)index{
    self.dataArray = [NSMutableArray arrayWithArray:array];
    self.selectIndex = index;
    CGFloat height = (array.count/2)*90+8;
    NSInteger count = array.count%4;
    if (count > 0) {
        height  = height + 90;
    }
    _collectview.height = height;
    [_collectview reloadData];
}


@end
